﻿namespace Hidistro.UI.AccountCenter.CodeBehind
{
    using ASPNET.WebControls;
    using Hidistro.Core.Enums;
    using Hidistro.UI.Common.Controls;
    using System;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Web.UI.WebControls;
    using Hidistro.Core;
    using Hidistro.Core.Entities;
    using Hidistro.Entities.Orders;
    using Hidistro.Entities.Sales;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Member;
    using Hidistro.UI.Common.Controls;
    using Hidistro.UI.SaleSystem.Tags;
    using System.Collections.Specialized;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Data;

    public class UserOrderManage_OrderList : AscxTemplatedWebControl
    {
        private Repeater rptOrderlist;
        private Pager pager;
        private IButton btnSearch;
        private TextBox txtKeyWord;
        private HiddenField hftype;

        protected override void AttachChildControls()
        {
            this.rptOrderlist = (Repeater)this.FindControl("rptOrderlist");
            this.rptOrderlist.ItemDataBound += rptOrderlist_ItemDataBound;
            this.rptOrderlist.ItemCommand += rptOrderlist_ItemCommand;
            this.pager = (Pager)this.FindControl("pager");

            this.btnSearch = ButtonManager.Create(this.FindControl("btnSearch"));
            this.txtKeyWord = (TextBox)this.FindControl("txtKeyWord");
            this.btnSearch.Click += new EventHandler(this.btnSearch_Click);
            this.hftype = (HiddenField)this.FindControl("hftype");

            BindOrders();
            
        }

        private void rptOrderlist_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Repeater GList = (Repeater)e.Item.FindControl("rptOrderlistSon");
                GList.ItemDataBound += rptOrderlistson_ItemDataBound;
                GList.ItemCommand += rptOrderlistson_ItemCommand;
                DataRowView rowv = (DataRowView)e.Item.DataItem;//找到分类Repeater关联的数据项 
                GList.DataSource = TradeHelper.GetOrderGoodsNew(rowv["OrderId"].ToString());
                GList.DataBind();
                OrderStatus status = (OrderStatus)DataBinder.Eval(e.Item.DataItem, "OrderStatus");
                ImageButton button2 = (ImageButton)e.Item.FindControl("lkbtnCloseOrder");
                button2.Visible = status == OrderStatus.WaitBuyerPay;
            }
        }

        private void rptOrderlistson_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                OrderStatus status = (OrderStatus)DataBinder.Eval(e.Item.DataItem, "OrderStatus");
                HtmlAnchor anchor2 = (HtmlAnchor)e.Item.FindControl("hlinkPay");
                anchor2.Visible = status == OrderStatus.WaitBuyerPay;
                HyperLink hl1 = (HyperLink)e.Item.FindControl("hlinkOrderDetails");
                if (status == OrderStatus.Closed)
                    hl1.Visible = false;
            }
        }
        protected void rptOrderlist_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            OrderInfo orderInfo = TradeHelper.GetOrderInfo(e.CommandArgument.ToString());
            if (orderInfo != null)
            {
                if ((e.CommandName == "CLOSE_TRADE") && orderInfo.CheckAction(OrderActions.SELLER_CLOSE))
                {
                    if (TradeHelper.CloseOrder(orderInfo.OrderId))
                    {
                        this.BindOrders();
                        this.ShowMessage("成功的关闭了该订单", true);
                    }
                    else
                    {
                        this.ShowMessage("关闭订单失败", false);
                    }
                }
            }
        }
        protected void rptOrderlistson_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            OrderInfo orderInfo = TradeHelper.GetOrderInfo(e.CommandArgument.ToString());
            if (orderInfo != null)
            {
                if ((e.CommandName == "FINISH_TRADE") && orderInfo.CheckAction(OrderActions.SELLER_FINISH_TRADE))
                {
                    if (TradeHelper.ConfirmOrderFinish1(orderInfo))
                    {
                        this.BindOrders();
                        this.ShowMessage("成功的完成了该订单", true);
                    }
                    else
                    {
                        this.ShowMessage("完成订单失败", false);
                    }
                }
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.ReloadOrders();
        }

        private void ReloadOrders()
        {
            NameValueCollection queryStrings = new NameValueCollection();
            queryStrings.Add("keyword", this.txtKeyWord.Text.Trim());
            queryStrings.Add("orderStatus", this.hftype.Value);
            //base.ReloadPage(queryStrings);
            this.Page.Response.Redirect(Globals.ApplicationPath + string.Format("/user/UserOrders.aspx?keyword={0}&hftype={1}", this.txtKeyWord.Text.Trim(),this.hftype.Value));
        }
        private void BindOrders()
        {
            Pagination page = new Pagination
            {
                PageIndex = this.pager.PageIndex,
                PageSize = this.pager.PageSize
            };
            string tags = string.Empty;
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["keyword"]))
            {
                tags = this.Page.Request.QueryString["keyword"];
            }

            this.txtKeyWord.Text = tags;

            OrderQuery orderQuery = this.GetOrderQuery();
            DbQueryResult userOrder = TradeHelper.GetUserOrder(HiContext.Current.User.UserId, orderQuery, tags);
            //DbQueryResult userOrder = TradeHelper.GetUserOrder(HiContext.Current.User.UserId, orderQuery);
            rptOrderlist.DataSource = userOrder.Data;
            rptOrderlist.DataBind();
            pager.TotalRecords = userOrder.TotalRecords;
            //OrderQuery orderQuery = this.GetOrderQuery();
            //DbQueryResult userOrder = TradeHelper.GetUserOrder(HiContext.Current.User.UserId, orderQuery);
            //this.userorderlist.DataSource = userOrder.Data;
            //this.userorderlist.DataBind();
            ////this.txtOrderId.Text = orderQuery.OrderId;
            ////this.txtShipId.Text = orderQuery.ShipId;
            ////this.txtShipTo.Text = orderQuery.ShipTo;
            ////this.dropOrderStatus.SelectedValue = orderQuery.Status;
            ////this.calendarStartDate.SelectedDate = orderQuery.StartDate;
            ////this.calendarEndDate.SelectedDate = orderQuery.EndDate;
            //this.pager.TotalRecords = userOrder.TotalRecords;
        }
        private OrderQuery GetOrderQuery()
        {
            OrderQuery query = new OrderQuery();
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["orderId"]))
            {
                query.OrderId = this.Page.Request.QueryString["orderId"];
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["shipId"]))
            {
                query.ShipId = this.Page.Request.QueryString["shipId"];
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["shipTo"]))
            {
                query.ShipTo = this.Page.Request.QueryString["shipTo"];
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["orderId"]))
            {
                query.OrderId = this.Page.Request.QueryString["orderId"];
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["startDate"]))
            {
                query.StartDate = new DateTime?(Convert.ToDateTime(this.Page.Server.UrlDecode(this.Page.Request.QueryString["startDate"])));
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["endDate"]))
            {
                query.EndDate = new DateTime?(Convert.ToDateTime(this.Page.Server.UrlDecode(this.Page.Request.QueryString["endDate"])));
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["orderStatus"]))
            {
                int result = 0;
                if (int.TryParse(this.Page.Request.QueryString["orderStatus"], out result))
                {
                    query.Status = (OrderStatus)result;
                }
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["sortBy"]))
            {
                query.SortBy = this.Page.Request.QueryString["sortBy"];
            }
            if (!string.IsNullOrEmpty(this.Page.Request.QueryString["sortOrder"]))
            {
                int num2 = 0;
                if (int.TryParse(this.Page.Request.QueryString["sortOrder"], out num2))
                {
                    query.SortOrder = (SortAction)num2;
                }
            }
            query.PageIndex = this.pager.PageIndex;
            query.PageSize = this.pager.PageSize;
            return query;
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/Common_UserCenter/Skin-UserOrderManage_OrderList.ascx";
            }
            base.OnInit(e);
        }
        public object DataSource
        {
            get
            {
                return this.rptOrderlist.DataSource;
            }
            set
            {
                this.EnsureChildControls();
                this.rptOrderlist.DataSource = value;
            }
        }
        public override void DataBind()
        {
            this.EnsureChildControls();
            if (this.rptOrderlist.DataSource != null) this.rptOrderlist.DataBind();
        }
    }
}

